package com.gamerole.app.ui

import android.content.Intent
import android.net.Uri
import android.provider.Browser
import android.view.View
import android.widget.Toast
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.paging.LoadState
import com.gamerole.app.R
import com.gamerole.app.databinding.AppFragmentArticleBinding
import com.gamerole.app.databinding.AppRecyItemChatBinding
import com.gamerole.app.entity.ArticleBean
import com.gamerole.app.page.adapter.ArticleAdapter
import com.gamerole.app.viewmodel.ChatViewModel
import com.gamerole.commom.adapter.ItemClickSupport
import com.gamerole.commom.adapter.slimadapter.SlimAdapter
import com.gamerole.commom.base.BaseFragment
import com.gamerole.commom.extention.image
import com.gamerole.commom.mvchelper.PageDataSource
import com.hi.dhl.binding.viewbind
import com.shizhefei.mvc.MVCSwipeRefreshHelper
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch


@AndroidEntryPoint
class ArticleFragment constructor(private var type: Int) :
    BaseFragment(R.layout.app_fragment_article) {
    private val viewModelChat: ChatViewModel by viewModels()
    override fun getViewModel() = viewModelChat
    private val binding: AppFragmentArticleBinding by viewbind()

    override fun lazyInitData() {
    }

    override fun initView() {
        var articleAdapter = ArticleAdapter()
        var items = viewModelChat.getPageData(type)
        binding.recyclerViewNested.adapter = articleAdapter
        lifecycleScope.launchWhenCreated {
            items.collectLatest {
                articleAdapter.submitData(it)
            }
        }
        binding.refreshLayout.setOnRefreshListener {
            articleAdapter.refresh()
        }
        articleAdapter.addLoadStateListener {
            when (it.refresh) {
                is LoadState.NotLoading -> {
                    // 根据LoadState，结束smartRefreshLayout的对应状态
                    binding.refreshLayout.isRefreshing = false
                }
                is LoadState.Loading -> {
                }
                is LoadState.Error -> {
                }
            }
        }

        ItemClickSupport.addTo(binding.recyclerViewNested)
            .setOnItemClickListener { recyclerView, position, v ->
                run {
                    val articleBean = articleAdapter.peek(position)!!
                    val intent = Intent(
                        Intent.ACTION_VIEW,
//                        Uri.parse(articleBean.link.replace("www", "m"))
                        Uri.parse("bilibili://bangumi/season/"+articleBean.season_id)
                    )
                    intent.putExtra(Browser.EXTRA_CREATE_NEW_TAB, true)
                    intent.putExtra(Browser.EXTRA_APPLICATION_ID, requireActivity().packageName)
                    requireActivity().startActivity(intent)
                }
            }

    }


}